package com.example.pufang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.pufang.mapper.LoginMapper;
import com.example.pufang.mapper.UserMapper;
import com.example.pufang.pojo.Login;
import com.example.pufang.pojo.User;
import com.example.pufang.service.EChartService;
import com.example.pufang.service.LoginService;
import com.example.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class EChartServiceImpl implements EChartService {

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Map<String, Object> getLoginEChartDayInfo() {

        // 需要获取的距离当前日期的总天数
        int days = 15;
        days -= 1;

        // 存储日期信息
        ArrayList<String> dayData = new ArrayList<>();
        // 存储登录数据信息
        ArrayList<Long> loginData = new ArrayList<>();
        // 存储注册数据信息
        ArrayList<Long> registerData = new ArrayList<>();

        LambdaQueryWrapper<Login> loginWrapper = new LambdaQueryWrapper<>();

        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();

        for (int i=days; i>=0; i--) {
            String beforeDay = DateUtils.getBeforeDay(i);
            loginWrapper.clear();
            loginWrapper.ge(Login::getCreateTime, beforeDay + " 00:00:00")
                    .le(Login::getCreateTime, beforeDay + " 23:59:59");

            // 存入数组
            dayData.add(beforeDay);
            loginData.add(loginMapper.selectCount(loginWrapper));

            userWrapper.clear();
            userWrapper.ge(User::getCreateTime, beforeDay + " 00:00:00")
                    .le(User::getCreateTime, beforeDay + " 23:59:59");
            registerData.add(userMapper.selectCount(userWrapper));
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("dayData", dayData);
        map.put("loginData", loginData);
        map.put("registerData", registerData);

        return map;
    }
}
